#include <user.h>

int
mm_test(void)
{
	 void *v1, *v2, *v3;

	 kprintf("Testing valloc\n");

	 v1 = alloc(PAGE_SIZE);
	 v2 = alloc(PAGE_SIZE * 2);
	 v3 = alloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);
	 
	 v1 = alloc(PAGE_SIZE);
	 v2 = alloc(PAGE_SIZE * 2);
	 v3 = alloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);
	 
	 v1 = alloc(PAGE_SIZE);
	 v2 = alloc(PAGE_SIZE * 2);
	 v3 = alloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);

	 kprintf("Testing balloc\n");

	 v1 = balloc(PAGE_SIZE);
	 v2 = balloc(PAGE_SIZE * 2);
	 v3 = balloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 bfree(v1); bfree(v2); bfree(v3);
	 
	 v1 = balloc(PAGE_SIZE);
	 v2 = balloc(PAGE_SIZE * 2);
	 v3 = balloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 bfree(v1); bfree(v2); bfree(v3);
	 
	 v1 = balloc(PAGE_SIZE);
	 v2 = balloc(PAGE_SIZE * 2);
	 v3 = balloc(PAGE_SIZE * 3);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 bfree(v1); bfree(v2); bfree(v3);

	 kprintf("Testing malloc\n");

	 v1 = alloc(8);
	 v2 = alloc(10);
	 v3 = alloc(13);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);
	 
	 v1 = alloc(8);
	 v2 = alloc(10);
	 v3 = alloc(13);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);
	 
	 v1 = alloc(8);
	 v2 = alloc(10);
	 v3 = alloc(13);
	 kprintf("v1 = %08x, v2 = %08x, v3 = %08x\n", v1, v2, v3);
	 free(v1); free(v2); free(v3);

	 return 0;
}

static void
__test_fiber2(void *_)
{
	 while (1)
	 {
		  kprintf("HELLO FROM THREAD PRIV %08x FIBER2\n", upriv);
		  fiber_yield();
	 }
}

static void
__test_fiber1_(void *_)
{
	 fiber_create(__test_fiber2, NULL);

	 while (1)
	 {
		  kprintf("HELLO FROM THREAD PRIV %08x FIBER1\n", upriv);
		  fiber_yield();
	 }
}

static void
__test_fiber1(void *_)
{
	 fiber_thread_create(__test_fiber1_, NULL);
	 fiber_create(__test_fiber2, NULL);

	 while (1)
	 {
		  kprintf("HELLO FROM THREAD PRIV %08x FIBER1\n", upriv);
		  fiber_yield();
	 }
}

static void
__test_fiber_notify(void *_)
{
	 kprintf("Here is fiber_notify, _ = %08x\n", _);
	 fiber_notify((fiber_t)_);
	 while (1) fiber_yield();
}

static void
__test_fiber_wait(void *_)
{
	 kprintf("Here is fiber_wait\n");
	 fio_uevent_touch();
	 fiber_yield();
	 
	 fiber_wait_pretend();
	 fiber_create(__test_fiber_notify, fiber_self());
	 fiber_wait_try();
	 kprintf("Here is fiber_wait, again\n");
	 while (1) fiber_yield();
}

void
user_entry(void)
{
	 mm_init();

	 while (1) ;
}
